from django.shortcuts import render, HttpResponse, redirect


# Create your views here.
def index(request):
    if request.method == 'POST':
        i1 = request.POST.get('i1')
        i2 = request.POST.get('i2')
        i3 = int(i1) + int(i2)
        return render(request, 'index.html', {'i1': i1, 'i2': i2, 'i3': i3})

    return render(request, 'index.html')


def calc(request):
    i1 = request.POST.get('x1')
    i2 = request.POST.get('x2')
    i3 = int(i1) + int(i2)
    return HttpResponse('/index/')


import json
from django.http.response import JsonResponse


def test(request):
    print(request.POST)
    # hobby = request.POST.getlist('hobby[]')
    # print(hobby,type(hobby))
    ret = json.loads(request.POST.get('hobby'))
    print(ret, type(ret))
    # int('aaaaa')
    # return HttpResponse(json.dumps({'status': 'ok', 'msg': 'xxxxxx'}))
    return JsonResponse({'status': 'ok', 'msg': 'xxxxxx'})


def upload(request):
    f1 = request.FILES.get('f1')
    with open(f1.name, 'wb') as f:
        for i in f1.chunks():
            f.write(i)

    return JsonResponse({'status': 'ok', 'msg': 'xxxxxx'})
